import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var txtHeadingAccuracy: UILabel!
    @IBOutlet weak var txtMagneticHeading: UILabel!
    @IBOutlet weak var txtTrueHeading: UILabel!
    @IBOutlet weak var txtHeadingX: UILabel!
    @IBOutlet weak var txtHeadingY: UILabel!
    @IBOutlet weak var txtHeadingZ: UILabel!
    
    @IBOutlet weak var txtGPSHorizontalAccuracy: UILabel!
    @IBOutlet weak var txtGPSVerticalAccuracy: UILabel!
    @IBOutlet weak var txtGPSLongitude: UILabel!
    @IBOutlet weak var txtGPSLatitude: UILabel!
    @IBOutlet weak var txtGPSHeight: UILabel!
    @IBOutlet weak var txtGPSSpeed: UILabel!
    
    var lm:CLLocationManager!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        lm=CLLocationManager()
        lm.delegate=self
        if lm.respondsToSelector(Selector("requestWhenInUseAuthorization")){
            lm.requestWhenInUseAuthorization()
        }
        lm.startUpdatingLocation()
    }
    //视图显示事件
    override func viewDidAppear(animated: Bool) {
        lm.startUpdatingHeading()
    }
    //
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //方位更新
    func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
        txtHeadingAccuracy.text = "精度：\(newHeading.headingAccuracy)"
        txtMagneticHeading.text = "磁航向：\(newHeading.magneticHeading)"
        txtTrueHeading.text = "真航向：\(newHeading.trueHeading)"
        txtHeadingX.text = "X：\(newHeading.x)"
        txtHeadingY.text = "Y：\(newHeading.y)"
        txtHeadingZ.text = "Z：\(newHeading.z)"
    }
    //位置更新
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location=locations.last {
            txtGPSHorizontalAccuracy.text = "水平精度：\(location.horizontalAccuracy)"
            txtGPSVerticalAccuracy.text = "垂直精度：\(location.verticalAccuracy)"
            txtGPSHeight.text = "高度：\(location.altitude)"
            txtGPSLatitude.text = "纬度：\(location.coordinate.latitude)"
            txtGPSLongitude.text = "经度：\(location.coordinate.longitude)"
            txtGPSSpeed.text = "速度：\(location.speed)"
        }else{
            
        }
    }
}